//
//  CVCellOfConsult.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/12.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "CVCellOfConsult.h"
#import "BaseButton.h"
#import "MJMacro.h"
#import "BaseCollectionView.h"
#import "CVCellOfTopic.h"
#import "CVCellOfTopicTwo.h"
@interface CVCellOfConsult()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) BaseCollectionView *collectionViewOfTopic;
@property (nonatomic, strong) UIView *blueView;
@end
@implementation CVCellOfConsult
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // 创建上面的 label
        [self createAboveLabel];
        // 创建 tableView
        [self createCollectView];
    }
    return self;
}
#pragma mark - 创建子上面的 label
- (void)createAboveLabel{
    BaseButton *firstButton = [BaseButton buttonWithType:UIButtonTypeSystem];
    firstButton.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, 44);
    firstButton.backgroundColor = [UIColor whiteColor];
    [firstButton setTitle:@"热门话题" forState:UIControlStateNormal];
    [firstButton addTarget:self action:@selector(clickHotTopic:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:firstButton];
    BaseButton *secondeButton = [BaseButton buttonWithType:UIButtonTypeSystem];
    secondeButton.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, 44);
    [secondeButton addTarget:self action:@selector(clickNewTopic:) forControlEvents:UIControlEventTouchUpInside];
    [secondeButton setTitle:@"最新话题" forState:UIControlStateNormal];
    secondeButton.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:secondeButton];
    self.blueView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 4 - 40, 42, 80, 2)];
    self.blueView.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:_blueView];
}
#pragma mark - 点击热门话题
- (void)clickHotTopic:(UIButton *)button{
    [UIView animateWithDuration:0.1 animations:^{
        self.blueView.frame = CGRectMake(SCREEN_WIDTH / 4 - 40, 42, 80, 2);
    }];
    _collectionViewOfTopic.contentOffset = CGPointMake(0, 0);
}
#pragma mark - 点击最新话题
- (void)clickNewTopic:(UIButton *)button{
    [UIView animateWithDuration:0.1 animations:^{
        
        self.blueView.frame = CGRectMake(SCREEN_WIDTH / 4 - 40 + SCREEN_WIDTH / 2, 42, 80, 2);
    }];
    _collectionViewOfTopic.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
}
#pragma mark - 滑动的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == 0) {
        [self clickHotTopic:nil];
    }
    if (scrollView.contentOffset.x == SCREEN_WIDTH) {
        [self clickNewTopic:nil];
    }
}
#pragma mark - 创建 CollectView
- (void)createCollectView{
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionViewOfTopic = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 44 - 49) collectionViewLayout:_layout];
    [self.contentView addSubview:_collectionViewOfTopic];
    self.collectionViewOfTopic.pagingEnabled = YES;
    self.collectionViewOfTopic.delegate = self;
    self.collectionViewOfTopic.dataSource = self;
    [self.collectionViewOfTopic registerClass:[CVCellOfTopic class] forCellWithReuseIdentifier:@"CVCellOfTopic"];
    [self.collectionViewOfTopic registerNib:[UINib nibWithNibName:@"CVCellOfTopicTwo" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CVCellOfTopicTwo"];
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.layout.itemSize = CGSizeMake(SCREEN_WIDTH, _collectionViewOfTopic.bounds.size.height);
    self.layout.minimumLineSpacing = 0;
    self.layout.minimumInteritemSpacing = 0;
    self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}

#pragma mark - collectionView 的 Row 的数量的确定
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}
#pragma mark - CollectView 的 cell 的赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CVCellOfTopic *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfTopic" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor magentaColor];
        return cell;
    } else {
        CVCellOfTopicTwo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfTopicTwo" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
        return cell;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
